<template>
  <el-dialog
    :title="!dataForm.expressId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="快递公司名" prop="expressName" label-width="150px">
        <el-input v-model="dataForm.expressName" placeholder="快递公司名"></el-input>
      </el-form-item>
	  <el-form-item label="公司代码" prop="expressCode" label-width="150px">
	    <el-input v-model="dataForm.expressCode" placeholder="公司代码"></el-input>
	  </el-form-item>
	  <el-form-item label="公司编码" prop="expressAbbre" label-width="150px">
	    <el-input v-model="dataForm.expressAbbre" placeholder="公司编码"></el-input>
	  </el-form-item>
	  <el-form-item label="公司网址" prop="expressSiteUrl" label-width="150px">
	    <el-input v-model="dataForm.expressSiteUrl" placeholder="公司网址"></el-input>
	  </el-form-item>
	  <el-form-item label="公司LOGO" prop="expressLogoUrl" label-width="150px">
	    <el-upload
	      class="avatar-uploader"
	      :action="uploadUrl"
	      :show-file-list="false"
	      :on-success="handleExpressImgSuccess"
	      :before-upload="beforeAvatarUpload">
	      <img v-if="expressImgUrl" :src="expressImgUrl" class="avatar">
	      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
	    </el-upload>
	  </el-form-item>
     <el-form-item label="状态" size="mini" prop="state" label-width="150px">
        <el-radio-group v-model="dataForm.state">
          <el-radio :label="0">启用</el-radio>
          <el-radio :label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNum" label-width="150px">
        <el-input-number v-model="dataForm.orderNum" controls-position="right" :min="0" label="排序号"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="submitVisible" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        submitVisible: false,
        dataForm: {
          expressId: '',
          expressName: '',
          expressCode: '',
          expressAbbre: '',
          expressSiteUrl: '',
          expressLogoUrl: '',
          state: ''
        },
        dataRule: {
          expressName: [
            { required: true, message: '快递公司名', trigger: 'blur' }
          ],
          expressCode: [
            { required: true, message: '公司代码', trigger: 'blur' }
          ],
          expressAbbre: [
            { required: true, message: '公司编码', trigger: 'blur' }
          ],
          expressSiteUrl: [
            { required: true, message: '公司网址', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '请设置商品筛选类别状态', trigger: 'blur' }
          ]
        },
        uploadUrl: '',
        expressImgUrl: ''
      }
    },
    methods: {
      init (id) {
        this.uploadUrl = this.$http.adornUrl(`/pig-file/center/file/upload`)
        this.dataForm.state = 0
        this.dataForm.expressId = id || ''
        this.dataForm.orderNum = 0
        this.visible = true
        this.submitVisible = false
        this.expressImgUrl = ''
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.expressId) {
          this.$http({
            url: this.$http.adornUrl(`/pig-center-biz/shopset/shopExpressInfo/info`),
            method: 'post',
            params: this.$http.adornParams({
              'id': this.dataForm.expressId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.expressName = data.shopExpressInfo.expressName
              this.dataForm.expressCode = data.shopExpressInfo.expressCode
              this.dataForm.expressAbbre = data.shopExpressInfo.expressAbbre
              this.dataForm.expressSiteUrl = data.shopExpressInfo.expressSiteUrl
              this.dataForm.state = data.shopExpressInfo.state
              this.dataForm.orderNum = data.shopExpressInfo.orderNum
              this.dataForm.expressLogoUrl = data.shopExpressInfo.expressLogoUrl
              // console.log(data.shopExpressInfo.expressLogoUrl)
              if (data.shopExpressInfo.expressLogoUrl) {
                this.expressImgUrl = this.loadImage(data.shopExpressInfo.expressLogoUrl)
              }
            }
          })
        }
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.submitVisible = true
            this.$http({
              url: this.$http.adornUrl(`/pig-center-biz/shopset/shopExpressInfo/${!this.dataForm.expressId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'expressId': this.dataForm.expressId || undefined,
                'expressName': this.dataForm.expressName,
                'expressCode': this.dataForm.expressCode,
                'expressAbbre': this.dataForm.expressAbbre,
                'expressSiteUrl': this.dataForm.expressSiteUrl,
                'expressLogoUrl': this.dataForm.expressLogoUrl,
                'orderNum': this.dataForm.orderNum,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 上传广告分类的图片
      handleExpressImgSuccess (res, file) {
        this.expressImgUrl = URL.createObjectURL(file.raw)
        if (res && res.code === 0) {
          this.dataForm.expressLogoUrl = res.url
        }
      },

      // 上传图片检查，必须是 jpg格式 大小不能超过2M
      beforeAvatarUpload (file) {
        // const isJPG = file.type === 'image/jpeg'
        // console.log(file.type)
        let temp = ['image/jpeg', 'image/jpg', 'mage/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png']
        const isJPG = temp.indexOf(file.type) > -1
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isJPG) {
          this.$message.error('上传图片格式不规范，只能是jpg png gif bmp格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      },
      loadImage (path) {
        return this.$http.adornUrl(`/pig-file/center/file/openImg?url=` + path)
      }
    }
  }
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 78px;
    height: 78px;
    line-height: 78px;
    text-align: center;
  }
  .avatar {
    width: 78px;
    height: 78px;
    display: block;
  }
</style>
